package com.glory.dgcpcpda.activity.check;

import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.entity.QCformEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class QC_TaskCreateActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.flType)
    FrameLayout flType;
    @BindView(R.id.ivType)
    ImageView ivType;
    @BindView(R.id.flOpportunity)
    FrameLayout flOpportunity;
    @BindView(R.id.ivOpportunity)
    ImageView ivOpportunity;
    @BindView(R.id.tvQCformID)
    TextView tvQCformID;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.tvCustomerCode)
    TextView tvCustomerCode;
    @BindView(R.id.tvMainmatType)
    TextView tvMainmatType;
    @BindView(R.id.tvCircuitName)
    TextView tvCircuitName;
    @BindView(R.id.tvType)
    TextView tvType;
    @BindView(R.id.tvTiming)
    TextView tvTiming;


    private FreeUI_GeneralPop mTypePop;
    private FreeUI_GeneralPop mOpportunityPop;
    private ArrayList<String> typeList = new ArrayList<>();
    private ArrayList<String> opportunityList = new ArrayList<>();


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_qc_taskcerate;
    }

    @Override
    protected void initView() {
        tvTitle.setText("QC任务创建");
        setMainLayout(llMain);
        tvBaseSubmit.setText("保存");
//        initEdit(etEqpID, s -> {
//            if (tvType.getText().toString().equals("")){
//                showToast("请先选择检验类型",false);
//                return;
//            }
//            if (tvTiming.getText().toString().equals("")){
//                showToast("请先选择检验时机",false);
//                return;
//            }
//            FreeUI_EntityCallBack<BaseResponseBean<QCformEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<QCformEntity>>
//                    (new TypeToken<BaseResponseBean<QCformEntity>>() {
//                    }.getType(), getSupportFragmentManager(), this) {
//                @Override
//                public void onSuccess(Response<BaseResponseBean<QCformEntity>> response) {
//                    super.onSuccess(response);
//                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
//                        tvQCformID.setText(response.body().getResponse().getBody().getQCFORM().getQCFORMID());
//                        tvStepDesc.setText(response.body().getResponse().getBody().getQCFORM().getSTEPDESC());
//                        tvCustomerCode.setText(response.body().getResponse().getBody().getQCFORM().getCUSTOMERCODE());
//                        tvMainmatType.setText(response.body().getResponse().getBody().getQCFORM().getMAINMATTYPE());
//                        tvCircuitName.setText(response.body().getResponse().getBody().getQCFORM().getCIRCUITNAME());
//                    }
//                }
//                @Override
//                public void onError(Response<BaseResponseBean<QCformEntity>> response) {
//                    super.onError(response);
//                    loadError(response.getException(), "QCFORMQUERY");
//                }
//            };
//
//
//
//            JsonObject map = new JsonObject();
//            map.addProperty("EQUIPMENTID", s);
//            String INSPECTTYPE = "";
//            switch (tvType.getText().toString()){
//                case "首检":
//                    INSPECTTYPE = "First";
//                    break;
//                case "巡检":
//                    INSPECTTYPE = "Patrol";
//                    break;
//            }
//            map.addProperty("INSPECTTYPE", INSPECTTYPE);
//            String INSPECTTIMING = "";
//            switch (tvTiming.getText().toString()){
//                case "交接班":
//                    INSPECTTIMING = "ChangeShift";
//                    break;
//                case "换工单":
//                    INSPECTTIMING = "ChangeWorkOrder";
//                    break;
//                case "设备维修":
//                    INSPECTTIMING = "Repair";
//                    break;
//                case "设备保养":
//                    INSPECTTIMING = "PM";
//                    break;
//            }
//            map.addProperty("INSPECTTIMING", INSPECTTIMING);
//            NetUtils.requestPostNet("CPC.QCFORMQUERY", "TRANSFERMES", this, map, callBack);
//        });
    }

    @Override
    protected void initData() {
        typeList.add("首检");
        typeList.add("巡检");

        opportunityList.add("交接班");
        opportunityList.add("换工单");
        opportunityList.add("设备维修");
        opportunityList.add("设备保养");
        opportunityList.add("换封装");
        opportunityList.add("换材料");
        opportunityList.add("换产品");
        opportunityList.add("重开机");
    }

    @OnClick({R.id.tvBaseSubmit,R.id.flType,R.id.flOpportunity})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:

                if (getText(etEqpID).equals("")){
                    showToast("分选机编号不能为空",false);
                    return;
                }

//                if (getText(etLot).equals("")){
//                    showToast("批次号不能为空",false);
//                    return;
//                }
                if (tvType.getText().toString().equals("")){
                    showToast("请选择检验类型",false);
                    return;
                }
                if (tvTiming.getText().toString().equals("")){
                    showToast("请选择检验时机",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<QCformEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<QCformEntity>>
                                (new TypeToken<BaseResponseBean<QCformEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<QCformEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                                    FreeUI_GeneralFragmentDialog tipDialog = FreeApi_DialogUtils.getConfirmDialog(MyApp.app, "确定", "QC任务创建成功,"+"单号："+response.body().getResponse().getBody().getQCFORMID(), () -> {
                                        finish();
                                    });
                                    tipDialog.show(getSupportFragmentManager(),"DIALOG");
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<QCformEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"QCFORMCREATE");
                            }
                        };
                        QCformEntity qCformEntity = new QCformEntity();
                        qCformEntity.setEQUIPMENTID(getText(etEqpID));
                        qCformEntity.setLOTID(getText(etLot));
                        String INSPECTTYPE = "";
                        switch (tvType.getText().toString()){
                            case "首检":
                                INSPECTTYPE = "First";
                                break;
                            case "巡检":
                                INSPECTTYPE = "Patrol";
                                break;
                        }
                        String INSPECTTIMING = "";
                        switch (tvTiming.getText().toString()){
                            case "交接班":
                                INSPECTTIMING = "ChangeShift";
                                break;
                            case "换工单":
                                INSPECTTIMING = "ChangeWorkOrder";
                                break;
                            case "设备维修":
                                INSPECTTIMING = "Repair";
                                break;
                            case "设备保养":
                                INSPECTTIMING = "PM";
                                break;
                            case "换封装":
                                INSPECTTIMING = "ChangeMainMatType";
                                break;
                            case "换材料":
                                INSPECTTIMING = "ChangeMaterial";
                                break;
                            case "换产品":
                                INSPECTTIMING = "ChangePart";
                                break;
                            case "重开机":
                                INSPECTTIMING = "ChangeReboot";
                                break;
                        }
                        qCformEntity.setINSPECTSTEPNAME(tvStepDesc.getText().toString());
                        qCformEntity.setINSPECTTIMING(INSPECTTIMING);
                        qCformEntity.setINSPECTTYPE(INSPECTTYPE);

                        JsonObject map = new JsonObject();
                        map.add("QCFORM", new JsonParser().parse(new Gson().toJson(qCformEntity)).getAsJsonObject());
                        NetUtils.requestPostNet("CPC.QCFORMCREATE", "TRANSFERMES", this, map, callBack);
                    }
                });
                break;
            case R.id.flType:
                if (mTypePop != null) {
                    if (mTypePop.isShowing()) {
                        mTypePop.dismiss();
                    }
                } else {
                    mTypePop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flType.getWidth(), typeList, ivType, (codeS, i) -> {
                        tvType.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivType);
                mTypePop.showAsDropDown(flType, 0, 0);
                break;
            case R.id.flOpportunity:
                if (mOpportunityPop != null) {
                    if (mOpportunityPop.isShowing()) {
                        mOpportunityPop.dismiss();
                    }
                } else {
                    mOpportunityPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flOpportunity.getWidth(), opportunityList, ivOpportunity, (codeS, i) -> {
                        tvTiming.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivOpportunity);
                mOpportunityPop.showAsDropDown(flOpportunity, 0, 0);
                break;
        }

    }
}
